using System.Collections.Generic;
using UnityEngine;

public class AIPool : MonoBehaviour
{
	public static AIPool instance;

	public List<AIController> AIList;

	public UnityEngine.Object aiplane_res;

	private float playerFightDis;

	private Transform PlayerTra;

	public Transform planeParent;

	public Transform shipParent;

	public Transform buildingParent;

	private GameObject tempobj;

	private AI_plane tempai_plane;

	private ShipController tempship;

	private BuildingAIController tempbuild;

	private int ailist_index;

	private int minimap_plane_index;

	private int minimap_ship_index;

	private int minimap_build_index;

	public bool ishaveboss;

	private AIController bossai;

	public int AiPlane_num;

	public int AiShip_num;

	public int AiBuild_num;

	private int[] LowQuality_limitnum = new int[3]
	{
		4,
		3,
		3
	};

	private string infostr;

	private Vector3 dirtemp;

	private int backindex;

	private List<AIController> canfightlist = new List<AIController>();

	public AIController curaimAI;

	private float tempdis;

	private float mindis;

	private int ai_active_num;

	private int build_num;

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		AiPlane_num = 0;
		AiShip_num = 0;
		AiBuild_num = 0;
	}

	public void initinfo(float dis, Transform tra)
	{
		playerFightDis = dis;
		PlayerTra = tra;
	}

	public void DisableAllAI()
	{
		for (int i = 0; i < AIList.Count; i++)
		{
			UnityVersionUtil.SetActiveRecursive(AIList[i].gameObject, state: false);
			AIList[i].isdie = true;
		}
		ishaveboss = false;
	}

	public void EnableAllAI()
	{
		for (int i = 0; i < AIList.Count; i++)
		{
			UnityVersionUtil.SetActiveRecursive(AIList[i].gameObject, state: true);
			AIList[i].isdie = false;
			AIList[i].Reset_AI();
		}
	}

	private void Update()
	{
		if (curaimAI == null)
		{
			setCurAim();
		}
		else
		{
			checkAIM();
		}
		showflaghp_frame_dir();
	}

	public List<planefightinfo> GetFightInfo()
	{
		List<planefightinfo> list = new List<planefightinfo>();
		for (int i = 0; i < AIList.Count; i++)
		{
			if (AIList[i].ai_type == AI_TYPE.aiplane)
			{
				planefightinfo planefightinfo = new planefightinfo();
				planefightinfo.planeindex = AIList[i].TypeIndex;
				planefightinfo.selfinfo = AIList[i].fightinfo;
				list.Add(planefightinfo);
			}
		}
		return list;
	}

	public bool check_list_num(int typeid)
	{
		switch (typeid)
		{
			case 0:
				for (int k = 0; k < AIList.Count; k++)
				{
					if (!AIList[k].isdie)
					{
						return false;
					}
				}
				return true;
			case 1:
				for (int j = 0; j < AIList.Count; j++)
				{
					if (!AIList[j].isdie && AIList[j].ai_type == AI_TYPE.aiplane)
					{
						return false;
					}
				}
				return true;
			case 2:
				for (int l = 0; l < AIList.Count; l++)
				{
					if (!AIList[l].isdie && AIList[l].ai_type != 0 && AIList[l].ai_type != AI_TYPE.aiship)
					{
						return false;
					}
				}
				return true;
			case 3:
				for (int i = 0; i < AIList.Count; i++)
				{
					if (!AIList[i].isdie && AIList[i].ai_type == AI_TYPE.aiship)
					{
						return false;
					}
				}
				return true;
			default:
				return false;
		}
	}

	public AIController ship_creat_plane()
	{
		for (int i = 0; i < AIList.Count; i++)
		{
			if (!UnityVersionUtil.IsActive(AIList[i].gameObject) && AIList[i].ai_type == AI_TYPE.aiplane)
			{
				return AIList[i];
			}
		}
		return null;
	}

	public void check_fightinfo(AI_TYPE dietype, int ai_index, int fri_index)
	{
		if (ai_index == -2)
		{
			return;
		}
		if (ai_index == -1)
		{
			plane_smothfollow.instance.change_fightinfo(dietype);
			if (GlobalInf.gamemode == TASKMODE.chanllenge)
			{
				UIController.instance.show_fight_info("你击败了 " + GlobalInf.ai_names[fri_index]);
			}
		}
		else
		{
			if (ai_index < 0 || ai_index >= AIList.Count)
			{
				return;
			}
			AIList[ai_index].change_fightinfo(dietype);
			if (GlobalInf.gamemode != TASKMODE.chanllenge)
			{
				return;
			}
			if (fri_index == -1)
			{
				switch (instance.AIList[ai_index].ai_type)
				{
					case AI_TYPE.aiplane:
						infostr = GlobalInf.ai_names[ai_index];
						break;
					case AI_TYPE.aiship:
						infostr = "战舰";
						break;
					case AI_TYPE.aibunker:
						infostr = "塔楼";
						break;
					case AI_TYPE.aihgtw:
						infostr = "塔楼";
						break;
				}
				infostr += " 击败了你";
				UIController.instance.show_fight_info(infostr);
				return;
			}
			if (GlobalInf.isInternalInjury)
			{
				UIController.instance.show_fight_info(GlobalInf.ai_names[ai_index] + " 击败 " + GlobalInf.ai_names[fri_index]);
				return;
			}
			switch (instance.AIList[ai_index].ai_type)
			{
				case AI_TYPE.aiplane:
					infostr = GlobalInf.ai_names[ai_index];
					break;
				case AI_TYPE.aiship:
					infostr = "战舰";
					break;
				case AI_TYPE.aibunker:
					infostr = "塔楼";
					break;
				case AI_TYPE.aihgtw:
					infostr = "塔楼";
					break;
			}
			switch (dietype)
			{
				case AI_TYPE.aiplane:
					infostr = infostr + " 击败 " + GlobalInf.fri_names[fri_index];
					break;
				case AI_TYPE.aiship:
					infostr += " 摧毁的战舰";
					break;
				case AI_TYPE.aibunker:
					infostr += " 摧毁的塔楼";
					break;
				case AI_TYPE.aihgtw:
					infostr += " 摧毁的塔楼";
					break;
				case AI_TYPE.building:
					infostr += " 摧毁了你的基地";
					break;
				case AI_TYPE.smallbuild:
					infostr += " 摧毁了你的基地";
					break;
				case AI_TYPE.aiboss:
					infostr += " 摧毁了你的基地";
					break;
			}
			UIController.instance.show_fight_info(infostr);
		}
	}

	public void aiplane_reset(Transform player_tra)
	{
		for (int i = 0; i < AIList.Count; i++)
		{
			if (!UnityVersionUtil.IsActive(AIList[i].gameObject) && AIList[i].ai_type == AI_TYPE.aiplane)
			{
				AIList[i].Reset_AI();
				tempobj = AIList[i].gameObject;
				tempobj.transform.forward = player_tra.forward;
				tempobj.transform.position = Cal_Pos_back(player_tra);
				UnityVersionUtil.SetActiveRecursive(AIList[i].gameObject, state: true);
			}
		}
	}

	public bool CheckBoss()
	{
		if (!bossai.isdie)
		{
			return true;
		}
		return false;
	}

	public void SetAllFalse()
	{
		for (int i = 0; i < AIList.Count; i++)
		{
			if (UnityVersionUtil.IsActive(AIList[i].gameObject) && !AIList[i].isdie && (AIList[i].ai_type == AI_TYPE.aibunker || AIList[i].ai_type == AI_TYPE.aihgtw))
			{
				AIList[i].Setdied();
			}
		}
	}

	public void creat_plane_fixed(int num, int attnum, List<PosRot> pr)
	{
		Attribute curatt = DataController.instance.DataAttribute[attnum];
		for (int i = 0; i < num; i++)
		{
			tempobj = null;
			backindex = check_ai_type(AI_TYPE.aiplane);
			if (backindex != -1)
			{
				tempobj = AIList[backindex].gameObject;
				UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
			}
			else if (GlobalInf.CurrentQuality > (Quality)101)
			{
				if (UnityEngine.Random.Range(0, 2) > 0)
				{
					AiPlane_num++;
					tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Planes/F22_AI")) as GameObject);
				}
				else
				{
					AiPlane_num++;
					tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Planes/F2000_AI")) as GameObject);
				}
			}
			else if (AiPlane_num < LowQuality_limitnum[0])
			{
				AiPlane_num++;
				tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Planes/F22_AI")) as GameObject);
			}
			if (tempobj == null)
			{
				break;
			}
			tempobj.transform.parent = planeParent;
			tempai_plane = tempobj.GetComponent<AI_plane>();
			tempai_plane.init_plane();
			tempai_plane.init_info(curatt);
			tempai_plane.changeHP();
			tempobj.transform.position = pr[i].pos;
			tempobj.transform.rotation = Quaternion.Euler(pr[i].rot);
			if (backindex == -1)
			{
				tempai_plane.TypeIndex = ailist_index;
				tempai_plane.minimapindex = minimap_plane_index;
				ailist_index++;
				minimap_plane_index++;
				AIList.Add(tempai_plane);
			}
		}
	}

	public void creat_plane_random(Transform playertr, int num, int attnum)
	{
		Attribute curatt = DataController.instance.DataAttribute[attnum];
		for (int i = 0; i < num; i++)
		{
			tempobj = null;
			backindex = check_ai_type(AI_TYPE.aiplane);
			if (backindex != -1)
			{
				tempobj = AIList[backindex].gameObject;
				UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
			}
			else if (GlobalInf.CurrentQuality > (Quality)101)
			{
				if (UnityEngine.Random.Range(0, 2) > 0)
				{
					AiPlane_num++;
					tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Planes/F22_AI")) as GameObject);
				}
				else
				{
					AiPlane_num++;
					tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Planes/F2000_AI")) as GameObject);
				}
			}
			else if (AiPlane_num < LowQuality_limitnum[0])
			{
				AiPlane_num++;
				tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Planes/F22_AI")) as GameObject);
			}
			tempobj.transform.parent = planeParent;
			tempai_plane = tempobj.GetComponent<AI_plane>();
			tempai_plane.init_plane();
			tempai_plane.init_info(curatt);
			tempai_plane.changeHP();
			if (GlobalInf.gamefirst)
			{
				tempobj.transform.forward = playertr.forward;
				tempobj.transform.position = Cal_Pos_guide(playertr);
			}
			else
			{
				tempobj.transform.forward = -playertr.forward;
				tempobj.transform.position = Cal_Pos(playertr);
			}
			if (backindex == -1)
			{
				tempai_plane.TypeIndex = ailist_index;
				tempai_plane.minimapindex = minimap_plane_index;
				ailist_index++;
				minimap_plane_index++;
				AIList.Add(tempai_plane);
			}
		}
	}

	public Vector3 Cal_Pos_guide(Transform bastra)
	{
		dirtemp = bastra.forward;
		dirtemp.y = 0f;
		dirtemp = dirtemp.normalized;
		return bastra.position + dirtemp * 0f + tempobj.transform.right * 100f + Vector3.up * 50f;
	}

	public Vector3 Cal_Pos(Transform bastra)
	{
		if (Vector3.Distance(bastra.position, GlobalInf.centerpoint) > (float)(GlobalInf.flyrange_AI + 1000))
		{
			dirtemp = bastra.forward;
			dirtemp.y = 0f;
			dirtemp = dirtemp.normalized;
			return GlobalInf.centerpoint + dirtemp * 3000f + tempobj.transform.right * UnityEngine.Random.Range(-20, 20) * 50f + Vector3.up * (450f + (float)UnityEngine.Random.Range(0, 10) * 20f);
		}
		dirtemp = bastra.forward;
		dirtemp.y = 0f;
		dirtemp = dirtemp.normalized;
		return bastra.position + dirtemp * 3000f + tempobj.transform.right * UnityEngine.Random.Range(-20, 20) * 50f + Vector3.up * (450f + (float)UnityEngine.Random.Range(0, 10) * 20f);
	}

	public Vector3 Cal_Pos_back(Transform bastra)
	{
		if (Vector3.Distance(bastra.position, GlobalInf.centerpoint) > (float)(GlobalInf.flyrange_AI + 1000))
		{
			dirtemp = bastra.forward;
			dirtemp.y = 0f;
			dirtemp = dirtemp.normalized;
			return GlobalInf.centerpoint + dirtemp * 3000f + tempobj.transform.right * UnityEngine.Random.Range(-20, 20) * 50f + Vector3.up * (450f + (float)UnityEngine.Random.Range(0, 10) * 20f);
		}
		dirtemp = -bastra.forward;
		dirtemp.y = 0f;
		dirtemp = dirtemp.normalized;
		return bastra.position + dirtemp * 3000f + tempobj.transform.right * UnityEngine.Random.Range(-20, 20) * 50f + Vector3.up * (450f + (float)UnityEngine.Random.Range(0, 10) * 20f);
	}

	public void creat_AI(NPC_ID_num id_num)
	{
		if (id_num.ID != -1 && id_num.num > 0)
		{
			switch (DataController.instance.DataNPC[id_num.ID].npctype)
			{
				case 0:
					init_aiplane(DataController.instance.DataNPC[id_num.ID].npcmode, id_num.num, DataController.instance.DataAttribute[DataController.instance.DataNPC[id_num.ID].attribute_id], id_num.pos_rot);
					break;
				case 1:
					init_aiship(DataController.instance.DataNPC[id_num.ID].npcmode, id_num.num, DataController.instance.DataAttribute[DataController.instance.DataNPC[id_num.ID].attribute_id], id_num.pos_rot);
					break;
				case 2:
					init_build(DataController.instance.DataNPC[id_num.ID].npcmode, id_num.num, DataController.instance.DataAttribute[DataController.instance.DataNPC[id_num.ID].attribute_id], id_num.pos_rot);
					break;
			}
		}
	}

	public void init_aiplane(int planetype, int planenum, Attribute curatt, List<PosRot> posrot)
	{
		for (int i = 0; i < planenum; i++)
		{
			tempobj = null;
			switch (planetype)
			{
				case 0:
					backindex = check_ai_name("F22_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality >= (Quality)101 || AiPlane_num < LowQuality_limitnum[0])
					{
						AiPlane_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Planes/F22_AI")) as GameObject);
					}
					break;
				case 1:
					if (GlobalInf.CurrentQuality < (Quality)101)
					{
						backindex = check_ai_name("F22_AI(Clone)");
						if (backindex != -1)
						{
							tempobj = AIList[backindex].gameObject;
							UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
						}
						else if (AiPlane_num < LowQuality_limitnum[0])
						{
							AiPlane_num++;
							tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Planes/F22_AI")) as GameObject);
						}
					}
					else
					{
						backindex = check_ai_name("F2000_AI(Clone)");
						if (backindex != -1)
						{
							tempobj = AIList[backindex].gameObject;
							UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
						}
						else
						{
							AiPlane_num++;
							tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Planes/F2000_AI")) as GameObject);
						}
					}
					break;
			}
			if (tempobj == null)
			{
				break;
			}
			tempobj.transform.parent = planeParent;
			tempai_plane = tempobj.GetComponent<AI_plane>();
			tempai_plane.init_plane();
			tempai_plane.init_info(curatt);
			tempai_plane.changeHP();
			if (posrot == null)
			{
				tempobj.transform.position = new Vector3(UnityEngine.Random.Range(0, 1000), 0f, UnityEngine.Random.Range(2200, 3000));
			}
			else
			{
				tempobj.transform.position = posrot[i].pos;
				tempobj.transform.rotation = Quaternion.Euler(posrot[i].rot);
			}
			if (backindex == -1)
			{
				tempai_plane.TypeIndex = ailist_index;
				tempai_plane.minimapindex = minimap_plane_index;
				ailist_index++;
				minimap_plane_index++;
				AIList.Add(tempai_plane);
			}
		}
	}

	public int check_ai_name(string strname)
	{
		for (int i = 0; i < AIList.Count; i++)
		{
			if (strname.Equals(AIList[i].transform.name) && !UnityVersionUtil.IsActive(AIList[i].gameObject))
			{
				return i;
			}
		}
		return -1;
	}

	public int check_ai_type(AI_TYPE needtype)
	{
		for (int i = 0; i < AIList.Count; i++)
		{
			if (AIList[i].ai_type == needtype && !UnityVersionUtil.IsActive(AIList[i].gameObject))
			{
				return i;
			}
		}
		return -1;
	}

	public void init_aiship(int shiptype, int shipnum, Attribute curatt, List<PosRot> posrot)
	{
		for (int i = 0; i < shipnum; i++)
		{
			tempobj = null;
			switch (shiptype)
			{
				case 0:
					backindex = check_ai_name("smallship_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiShip_num < LowQuality_limitnum[1])
					{
						AiShip_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Ships/smallship_AI")) as GameObject);
					}
					break;
				case 1:
					backindex = check_ai_name("bigship_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiShip_num < LowQuality_limitnum[1])
					{
						AiShip_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Ships/bigship_AI")) as GameObject);
					}
					break;
				case 2:
					backindex = check_ai_name("mujian_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiShip_num < LowQuality_limitnum[1])
					{
						AiShip_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Ships/mujian_AI")) as GameObject);
					}
					break;
			}
			if (tempobj == null)
			{
				break;
			}
			tempobj.transform.parent = shipParent;
			tempship = tempobj.GetComponent<ShipController>();
			if (backindex == -1)
			{
				tempship.TypeIndex = ailist_index;
				tempship.minimapindex = minimap_ship_index;
				ailist_index++;
				minimap_ship_index++;
				AIList.Add(tempship);
			}
			tempship.init_ship();
			tempship.init_info(curatt);
			tempship.changeHP();
			if (posrot == null)
			{
				tempobj.transform.position = new Vector3(UnityEngine.Random.Range(0, 1000), 0f, UnityEngine.Random.Range(2200, 3000));
				continue;
			}
			tempobj.transform.position = posrot[i].pos;
			tempobj.transform.rotation = Quaternion.Euler(posrot[i].rot);
		}
	}

	public void init_build(int buildtype, int buildnum, Attribute curatt, List<PosRot> posrot)
	{
		for (int i = 0; i < buildnum; i++)
		{
			tempobj = null;
			switch (buildtype)
			{
				case 0:
					backindex = check_ai_name("hgtw_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiBuild_num < LowQuality_limitnum[2])
					{
						AiBuild_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Building/hgtw_AI")) as GameObject);
					}
					break;
				case 1:
					backindex = check_ai_name("bunker_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiBuild_num < LowQuality_limitnum[2])
					{
						AiBuild_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Building/bunker_AI")) as GameObject);
					}
					break;
				case 2:
					backindex = check_ai_name("zhihuibu_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiBuild_num < LowQuality_limitnum[2])
					{
						AiBuild_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Building/zhihuibu_AI")) as GameObject);
					}
					ishaveboss = true;
					bossai = tempobj.GetComponent<BuildingAIController>();
					break;
				case 3:
					backindex = check_ai_name("Xinhaota_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiBuild_num < LowQuality_limitnum[2])
					{
						AiBuild_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Building/Xinhaota_AI")) as GameObject);
					}
					break;
				case 4:
					backindex = check_ai_name("LeiDa_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiBuild_num < LowQuality_limitnum[2])
					{
						AiBuild_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Building/LeiDa_AI")) as GameObject);
					}
					break;
				case 5:
					backindex = check_ai_name("fangzi1_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiBuild_num < LowQuality_limitnum[2])
					{
						AiBuild_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Building/fangzi1_AI")) as GameObject);
					}
					break;
				case 6:
					backindex = check_ai_name("fangzi2_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiBuild_num < LowQuality_limitnum[2])
					{
						AiBuild_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Building/fangzi2_AI")) as GameObject);
					}
					break;
				case 7:
					backindex = check_ai_name("seabuild1_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiBuild_num < LowQuality_limitnum[2])
					{
						AiBuild_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Building/seabuild1_AI")) as GameObject);
					}
					break;
				case 8:
					backindex = check_ai_name("seabuild2_AI(Clone)");
					if (backindex != -1)
					{
						tempobj = AIList[backindex].gameObject;
						UnityVersionUtil.SetActiveRecursive(tempobj.gameObject, state: true);
					}
					else if (GlobalInf.CurrentQuality > (Quality)101 || AiBuild_num < LowQuality_limitnum[2])
					{
						AiBuild_num++;
						tempobj = (UnityEngine.Object.Instantiate(Resources.Load("Building/seabuild2_AI")) as GameObject);
					}
					break;
			}
			if (tempobj == null)
			{
				break;
			}
			tempobj.transform.parent = buildingParent;
			tempbuild = tempobj.GetComponent<BuildingAIController>();
			if (backindex == -1)
			{
				tempbuild.TypeIndex = ailist_index;
				tempbuild.minimapindex = minimap_build_index;
				ailist_index++;
				minimap_build_index++;
				AIList.Add(tempbuild);
			}
			tempbuild.init_build();
			tempbuild.init_info(curatt);
			tempbuild.changeHP();
			if (posrot == null)
			{
				tempobj.transform.position = new Vector3(UnityEngine.Random.Range(0, 1000), 0f, UnityEngine.Random.Range(2200, 3000));
				continue;
			}
			tempobj.transform.position = posrot[i].pos;
			tempobj.transform.rotation = Quaternion.Euler(posrot[i].rot);
		}
	}

	public void setCurAim()
	{
		if (AIList.Count <= 0)
		{
			UIController.instance.setAIaim(Vector3.zero, istrue: false);
			UIController.instance.missileframe_yellow(Vector3.zero, 0, istrue: false);
			return;
		}
		canfightlist.Clear();
		for (int i = 0; i < AIList.Count; i++)
		{
			if (!AIList[i].isdie && Vector3.Distance(PlayerTra.position, AIList[i].m_transform.position) < playerFightDis)
			{
				Vector3 vector = Camera.main.WorldToScreenPoint(AIList[i].m_transform.position);
				if (vector.z > 0f)
				{
					canfightlist.Add(AIList[i]);
				}
			}
		}
		if (canfightlist.Count <= 0)
		{
			UIController.instance.setAIaim(Vector3.zero, istrue: false);
			UIController.instance.missileframe_yellow(Vector3.zero, 0, istrue: false);
			return;
		}
		mindis = float.PositiveInfinity;
		curaimAI = null;
		for (int j = 0; j < canfightlist.Count; j++)
		{
			if (Vector3.Angle(canfightlist[j].m_transform.position - PlayerTra.position, PlayerTra.forward) <= 30f)
			{
				tempdis = Vector3.Distance(PlayerTra.position, canfightlist[j].m_transform.position);
				if (mindis > tempdis)
				{
					mindis = tempdis;
					curaimAI = canfightlist[j];
				}
			}
		}
		if (curaimAI == null)
		{
			UIController.instance.setAIaim(Vector3.zero, istrue: false);
			UIController.instance.missileframe_yellow(Vector3.zero, 0, istrue: false);
		}
	}

	public void checkAIM()
	{
		if (Vector3.Distance(PlayerTra.position, curaimAI.m_transform.position) <= playerFightDis * 0.5f)
		{
			curaimAI.showAim_ai();
		}
		else
		{
			UIController.instance.setAIaim(Vector3.zero, istrue: false);
		}
		curaimAI.showMissileAI_yellow();
		if (Vector3.Distance(PlayerTra.position, curaimAI.m_transform.position) > playerFightDis || Vector3.Angle(curaimAI.m_transform.position - PlayerTra.position, PlayerTra.forward) > 30f)
		{
			curaimAI = null;
		}
		if (curaimAI != null && curaimAI.isdie)
		{
			curaimAI = null;
		}
	}

	public void showflaghp_frame_dir()
	{
		ai_active_num = 0;
		build_num = 0;
		plane_smothfollow.instance.curAIList.Clear();
		for (int i = 0; i < AIList.Count; i++)
		{
			if (!AIList[i].isdie)
			{
				AIList[i].showHPanddir();
				AIList[i].showframe();
				AIList[i].minimapflag();
				ai_active_num++;
				if (AIList[i].ai_type != 0 && AIList[i].ai_type != AI_TYPE.aiship)
				{
					build_num++;
				}
			}
			else
			{
				AIList[i].disappearhp_dir();
				AIList[i].disappearframe();
				AIList[i].minimapflag_dis();
			}
			if (plane_smothfollow.instance.curAIList.Count < 3 && !AIList[i].isdie && Vector3.Distance(PlayerTra.position, AIList[i].m_transform.position) < 0.5f * playerFightDis)
			{
				if (UIController.instance.missilelockai_red(i, istrue: true))
				{
					plane_smothfollow.instance.curAIList.Add(AIList[i]);
				}
			}
			else
			{
				UIController.instance.missilelockai_red(i, istrue: false);
			}
			if (plane_smothfollow.instance.curAIList.Count > 0 && UIController.instance.weaponUIcon.cur_missile > 0)
			{
				UIController.instance.setred_flag_showbtn(state: true);
			}
			else
			{
				UIController.instance.setred_flag_showbtn(state: false);
			}
		}
		if (GlobalInf.gamefirst)
		{
			return;
		}
		if (GlobalInf.gamemode == TASKMODE.level)
		{
			if (FightingTask.instance != null)
			{
				FightingTask.instance.CheckNum(ai_active_num);
			}
			return;
		}
		if (SurvivalChanllenge.instance != null)
		{
			SurvivalChanllenge.instance.CheckNum(ai_active_num);
		}
		if (DefendTheBase.instance != null)
		{
			DefendTheBase.instance.CheckbuildNum(build_num);
		}
	}

	public AIController calaitarget(Vector3 fri_trans_pos, bool isplane, int listindex = -1, bool isai = false)
	{
		if (AIList.Count == 0)
		{
			return null;
		}
		float num = float.PositiveInfinity;
		int index = 0;
		bool flag = false;
		for (int i = 0; i < AIList.Count; i++)
		{
			if (AIList[i].isdie || listindex == i)
			{
				continue;
			}
			if (isplane)
			{
				float num2;
				if (AIList[i].ai_type == AI_TYPE.aiplane)
				{
					num2 = Vector3.Distance(fri_trans_pos, AIList[i].m_transform.position);
					if (num2 < num)
					{
						num = num2;
						index = i;
						flag = true;
					}
					continue;
				}
				num2 = Vector3.Distance(fri_trans_pos, AIList[i].m_transform.position);
				if (num2 < num)
				{
					num = num2;
					index = i;
					flag = true;
				}
				if (UnityEngine.Random.Range(0, 10) > 7)
				{
					num = 0f;
					index = i;
					flag = true;
				}
			}
			else if (AIList[i].ai_type == AI_TYPE.aiplane)
			{
				float num2 = Vector3.Distance(fri_trans_pos, AIList[i].m_transform.position);
				if (num2 < num)
				{
					num = num2;
					index = i;
					flag = true;
				}
			}
		}
		if (isai && Vector3.Distance(plane_smothfollow.instance.m_Transform.position, fri_trans_pos) < num)
		{
			return null;
		}
		if (!flag)
		{
			return null;
		}
		return AIList[index];
	}

	private void OnDestroy()
	{
		if (instance != null)
		{
			instance = null;
		}
	}
}
